﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Text.RegularExpressions;
using System.Net;
using com.lover.common;
using System.IO;
using com.lover.astd.common.model;
using com.lover.astd.common.manager;
using com.lover.common.http;

namespace com.lover.astd.common.partner.impl
{
    public class _37WanLogin : LoginImplBase
    {
        public _37WanLogin() : base() { }
        public override LoginResult login(ref List<Cookie> cookies, string verify_code = null, string extra = null)
        {
            LoginResult result = new LoginResult();
            logging();
            string encode_username = Uri.EscapeDataString(_acc.UserName);
            string encode_password = CommonUtils.generateStringMd5(_acc.Password);
            encode_password = encode_password.ToLower();
            HttpResult resp;

            string pre_cookie_url = "http://www.37.com/gift/index.php";
            resp = TransferMgr.doGetPure(pre_cookie_url, ref cookies);
            //cook.Add(new Uri("http://www.37wan.com"), new Cookie("Hm_lvt_a0363bde807474cd65ce20feaaecfa13", TimeMgr.TimeStamp.ToString(), "/", "37wan.com"));
            //cook.Add(new Uri("http://www.37wan.com"), new Cookie("Hm_lpvt_a0363bde807474cd65ce20feaaecfa13", TimeMgr.TimeStamp.ToString(),"/", "37wan.com"));

            string loginurl = String.Format(
                "http://my.37.com/api/login.php?callback=jQuery18306438154398316079_{0}&action=login&login_account={1}&password={2}&ajax=0&remember_me=0&_={0}",
                TimeMgr.TimeStamp, encode_username, _acc.Password);
            
            resp = TransferMgr.doGetPure(loginurl, ref cookies, "http://www.37.com");
            if (resp == null) { result.StatusCode = LoginStatusCode.FailInLogin; return result; }

            string retstr = resp.getContent();

            if (retstr.IndexOf("alert") >= 0)
            {
                result.StatusCode = LoginStatusCode.FailInLogin;
                return result;
            }

            string redirecturl = String.Format("http://astd.37.com/entergame.php?server=S{0}&tuitan=", _acc.ServerId);

            processRedirect(redirecturl, result, ref cookies);
            return result;
        }
    }
}
